<template>
  <div class="wrap">
    <h1 class="title"><span class="gradient-text">网页配置</span> </h1>

    <!-- 列表 -->
    <div v-if="step === 'list'">
      <div class="header">
        <el-button type="primary" size="small" @click="step = 'config'">
          ＋ 新建配置
        </el-button>
      </div>

      <div class="list">
        <div v-for="item in list" :key="item.id" class="card">
          <div class="left">
            <img :src="item.backgroundUrl" class="mini-bg" />
          </div>

          <!-- 按钮区：右对齐，不覆盖文字 -->
          <div class="right-controls">
            <!-- 方案 A 标题 -->
            <span class="title-text">{{ item.title }}</span>
            <el-button size="mini" @click="openPreview(item.id)">预览</el-button>
          </div>
        </div>
      </div>
    </div>

    <!-- 新建配置 -->
    <page-config-editor v-if="step === 'config'" key="config" @created="onConfigCreated" @cancel="backToList" />

    <!-- 新建子项 -->
    <page-config-item-editor v-if="step === 'item'" :config-id="currentConfigId" key="item" @saved="backToList"
      @cancel="backToList" />
  </div>
</template>

<script>
import PageConfigEditor from './PageConfigEditor.vue'
import PageConfigItemEditor from './PageConfigItemEditor.vue'
import { getList } from '../../api/index'

export default {
  name: 'PageConfigItemTodo',
  components: { PageConfigEditor, PageConfigItemEditor },
  data() {
    return {
      step: 'list',
      currentConfigId: null,
      list: []
    }
  },
  watch: {
    '$route.path': {
      immediate: true,
      handler(val) {
        console.log(val)
        if (val === '/table') {
          this.step = 'list'
          this.currentConfigId = null
          this.fetch()
        }
      }
    }
  },
  methods: {
    async fetch() {
      const res = await getList()
      this.list = res.data
    },
    onConfigCreated(id) {
      this.currentConfigId = id
      this.step = 'item'
    },
    backToList() {
      // this.$router.push('/table')   // ← 唯一关键改动
      this.step = "list";
      this.currentConfigId = null;
      this.fetch()
    },
    openPreview(id) {
      window.open(`http://localhost:8085/preview/${id}`, '_blank')
    }
  }
}
</script>

<style>
/*======== 全局变量 ========*/
:root {
  --bg-1: #0d1117;
  --bg-2: #161b22;
  --accent-1: #6366f1;
  --accent-2: #a855f7;
  --accent-3: #ec4899;
  --card-bg: rgba(255, 255, 255, 0.05);
  --neon: 0 0 2px #fff, 0 0 4px #fff, 0 0 6px var(--accent-1), 0 0 8px var(--accent-1);
  --shadow: 0 8px 32px rgba(99, 102, 241, 0.18);
  --shadow-hover: 0 0 20px var(--accent-1), 0 0 40px var(--accent-2);
  --radius: 24px;
  --done: #22c55e;
  --del: #ef4444;
  --font: "Inter", sans-serif;
}

/*======== 基础 =========*/
* {
  box-sizing: border-box;
}

.content img,
.content video {
  max-width: 100%;
  max-height: 120px;
  width: auto;
  height: auto;
  border-radius: 8px;
  object-fit: cover;
  margin: 0 auto;
  display: block;
}

body {
  margin: 0;
  font-family: var(--font);
  background: linear-gradient(135deg, var(--bg-1) 0%, var(--bg-2) 100%);
  min-height: 100vh;
  color: #e5e7eb;
}

/*======== 标题 =========*/
@import url("https://fonts.googleapis.com/css2?family=Inter:wght@600 ;700&display=swap");

.title {
  text-align: center;
  font-size: 52px;
  font-weight: 700;
  letter-spacing: 3px;
  margin: 80px 0 50px;
  background: linear-gradient(90deg, var(--accent-1), var(--accent-2), var(--accent-3));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: glow 3s ease-in-out infinite alternate;
}

@keyframes glow {
  from {
    text-shadow: 0 0 10px var(--accent-1), 0 0 20px var(--accent-2);
  }
  to {
    text-shadow: 0 0 20px var(--accent-2), 0 0 30px var(--accent-3);
  }
}

/*======== 顶部按钮 =========*/
.header {
  display: flex;
  justify-content: center;
  margin-bottom: 50px;
}

.header .el-button {
  border: none;
  background: linear-gradient(90deg, var(--accent-1), var(--accent-2));
  color: #fff;
  padding: 16px 32px;
  border-radius: 50px;
  font-size: 16px;
  font-weight: 600;
  box-shadow: var(--shadow);
  transition: transform 0.3s, box-shadow 0.3s;
}

.header .el-button:hover {
  transform: translateY(-4px);
  box-shadow: var(--shadow-hover);
}

/*======== 玻璃卡片列表 =========*/
.list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(380px, 1fr));
  gap: 32px;
  padding: 0 24px;
  max-width: 1300px;
  margin: 0 auto;
}

.card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  background: var(--card-bg);
  border-radius: var(--radius);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: var(--shadow);
  transition: transform 0.3s, box-shadow 0.3s;
}

.card:hover {
  box-shadow: var(--shadow-hover);
  transform: translateY(-8px);
}

.left {
  display: flex;
  align-items: center;
  flex: 1;
}

.mini-bg {
  width: 64px;
  height: 64px;
  border-radius: 12px;
  object-fit: cover;
  margin-right: 16px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
}

/*======== 右侧控制区 =========*/
.right-controls {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-left: auto;
  flex-shrink: 0;
}

/*======== 方案 A：赛博渐变 + 微光 =========*/
.title-text {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0.5px;
  background: linear-gradient(90deg, var(--accent-2) 0%, var(--accent-3) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  position: relative;
  overflow: hidden;
  cursor: default;
  transition: transform 0.3s;
  max-width: 200px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-left: 10px;
}

.right-controls:hover .title-text {
  transform: scale(1.05);
}

.title-text::after {
  content: "";
  position: absolute;
  top: 0;
  left: -100%;
  width: 60%;
  height: 100%;
  background: linear-gradient(90deg,
              transparent 0%,
              rgba(255, 255, 255, 0.6) 50%,
              transparent 100%);
  animation: shine 3s infinite linear;
}

@keyframes shine {
  0% { left: -100%; }
  100% { left: 150%; }
}

/*======== 按钮美化 =========*/
.el-button--mini {
  border-radius: 20px;
  padding: 6px 16px;
  font-weight: 600;
}
</style>